<template>
    <div class="LoginContainer">
        <div :class="{'dowebok':true,'right-panel-active':isActive}" id="dowebok">
            <div class="form-container sign-up-container">
                <el-form 
                    :rules="rules" 
                    :model="user" 
                    ref="LoginRef">
                    <h1>登录</h1>
                    <p></p>
                    <el-form-item prop="username">
                        <el-input v-model="user.username" style="width: 300px" type="text" placeholder="请输入账号">
                            <template #prefix>
                                <el-icon><User /></el-icon>
                            </template>
                        </el-input>
                    </el-form-item>
                    <p></p>
                    <el-form-item prop="password">
                        <el-input v-model="user.password" style="width: 300px" type="password" placeholder="请输入密码" show-password>
                            <template #prefix>
                                <el-icon><Lock /></el-icon>
                            </template>
                        </el-input>
                    </el-form-item>
                    <p></p>
                    <el-form-item prop="code" style="display: flex;">
                        <el-input v-model="user.code" style="flex: 1;" type="password" placeholder="请输入验证码" />
                        <img :src="captchaUrl" @click="updateCaptcha" style="flex: 1; cursor: pointer;" />
                    </el-form-item>
                    <button @click="handleLogin">登录</button>
                  </el-form>
            </div>
            <div class="form-container sign-in-container">
                <el-form :rules="rules" :model="user" ref="RegisterRef">
                    <h1>注册</h1>
                    <p></p>
                    <el-form-item prop="username">
                        <el-input v-model="user.username" style="width: 300px" type="text" placeholder="请输入账号" show-password>
                            <template #prefix>
                                <el-icon><User /></el-icon>
                            </template>
                        </el-input>
                    </el-form-item>
                    <p></p>
                    <el-form-item prop="password">
                        <el-input v-model="user.password" style="width: 300px" type="password" placeholder="请输入密码" show-password>
                            <template #prefix>
                                <el-icon><Lock /></el-icon>
                            </template>
                        </el-input>
                    </el-form-item>
                    <p></p>
                    <el-form-item prop="email">
                        <el-input v-model="user.email" style="width: 300px" type="text" placeholder="请输入邮箱" show-password>
                            <template #prefix>
                                <el-icon><Film /></el-icon>
                            </template>
                        </el-input>
                    </el-form-item>
                    <p></p>
                    <button @click="handleRegister">注册</button>
                </el-form>
            </div>
            <div class="overlay-container">
                <div class="overlay">
                    <div class="overlay-panel overlay-left">
                        <h1>没有帐号？</h1>
                        <p>请先成为开放平台一员！</p>
                        <el-icon><Opportunity /></el-icon>
                        <button class="ghost" id="signIn" @click="isActive=false">注册</button>
                    </div>
                    <div class="overlay-panel overlay-right">
                        <h1>已有帐号？</h1>
                        <p>快速登录,感受平台魅力</p>
                        <button class="ghost" id="signUp" @click="isActive=true">登录</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref, inject } from "vue";
import { useRouter } from 'vue-router';

const router = useRouter();
const $post = inject('$post');
var captchaUrl=ref("http://localhost:8081/captcha")
var isActive = ref(false);
var user = reactive({
    username: '',
    password: '',
    email: '',
    code: ''
});

const LoginRef = ref(null); 
const RegisterRef = ref(null);

// 表单校验规则
const rules = reactive({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 15, message: '长度为3到15', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 15, message: '长度为3到15', trigger: 'blur' },
    ],
    email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' },
    ],
    code:[
      { required: true, message: '请输入验证码', trigger: 'blur' },
    ]
});

async function handleLogin() {
    LoginRef.value.validate(async (valid) => {
        if (valid) {
            try {
                const response = await $post("/user/login", user);
                sessionStorage.setItem("tokenStr", response.data);
                if (response.code == "200") {
                    ElMessage.success(response.message);
                    setTimeout(() => {
                        router.replace("/home");
                    }, 1000);
                } else {
                    ElMessage.error(response.message);
                }
            } catch (error) {
                console.error('登录失败:', error.response ? error.response.data : error.message);
                throw error;
            }
        } else {
            ElMessage.error('数据输入不合法');
        }
    });
}

async function handleRegister() {
    RegisterRef.value.validate(async (valid) => {
        if (valid) {
            try {
                const response = await $post("/user/register", user);
                if (response.code == "200") {
                    sessionStorage.setItem("tokenStr", response.data);
                    ElMessage.success(response.message);
                    user.username = '';
                    user.password = '';
                    user.email = '';
                    setTimeout(() => {
                        router.push("/home");
                    }, 1000);
                } else {
                    ElMessage.error(response.message);
                }
            } catch (error) {
                console.error('注册失败:', error.response ? error.response.data : error.message);
                throw error;
            }
        } else {
            ElMessage.error('数据输入不合法');
        }
    });
}

//切换验证码
function updateCaptcha(){
    captchaUrl.value = "http://localhost:8081/captcha?time=" + new Date().getTime();
}
</script>



<style soped>
        
* {
    box-sizing: border-box;
    margin:0;
    padding: 0;
}

.LoginContainer {
    font-family: 'Montserrat', sans-serif;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100vh;
    width: 100%;
    background: url(https://img.zcool.cn/community/01237c5bf51747a80121ab5d5d9a70.jpg) no-repeat fixed;
    background-size: cover;
    background-size: 100% 100%;
    
}

h1 {
    font-weight: bold;
    margin: 0;
}

p {
    font-size: 14px;
    line-height: 20px;
    letter-spacing: .5px;
    margin: 20px 0 30px;
}

span {
    font-size: 12px;
}

a {
    color: #333;
    font-size: 14px;
    text-decoration: none;
    margin: 15px 0;
}


.dowebok {
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 14px 28px rgba(0, 0, 0, .25), 0 10px 10px rgba(0, 0, 0, .22);
    position: relative;
    overflow: hidden;
    width: 768px;
    max-width: 100%;
    min-height: 480px;
}

.form-container form {
    background: #fff;
    display: flex;
    flex-direction: column;
    padding:  0 50px;
    height: 95%;
    justify-content: center;
    align-items: center;
    text-align: center;
}
.remeber{
    height: 5%;
    background: red;
}

.social-container {
    margin: 20px 0;
}

.social-container a {
    border: 1px solid #ddd;
    border-radius: 50%;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    margin: 0 5px;
    height: 40px;
    width: 40px;
}

.social-container a:hover {
    background-color: #eee;
}



/* 按钮css */
button {
    border-radius: 20px;
    border: 1px solid #ff4b2b;
    background: #ff4b2b;
    color: #fff;
    font-size: 12px;
    font-weight: bold;
    padding: 12px 45px;
    letter-spacing: 1px;
    text-transform: uppercase;
    transition: transform 80ms ease-in;
    cursor: pointer;
}

button:active {
    transform: scale(.95);
}

button:focus {
    outline: none;
}

button.ghost {
    background: transparent;
    border-color: #fff;
}

.form-container {
    display: flex;
    position: absolute;
    top: 0;
    height: 100%;
    transition: all .6s ease-in-out;
}

.sign-in-container {
    left: 0;
    width: 50%;
    z-index: 2;
}

.sign-up-container {
    left: 0;
    width: 50%;
    z-index: 1;
    opacity: 0;
}

.overlay-container {
    position: absolute;
    top: 0;
    left: 50%;
    width: 50%;
    height: 100%;
    overflow: hidden;
    transition: transform .6s ease-in-out;
    z-index: 100;
}

.overlay {
    background: #1d77ff;
    background: linear-gradient(to right, #21d7ff, #266bff) no-repeat 0 0 / cover;
    color: #fff;
    position: relative;
    left: -100%;
    height: 100%;
    width: 200%;
    transform: translateY(0);
    transition: transform .6s ease-in-out;
}

.overlay-panel {
    position: absolute;
    top: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 0 40px;
    height: 100%;
    width: 50%;
    text-align: center;
    transform: translateY(0);
    transition: transform .6s ease-in-out;
}

.overlay-right {
    right: 0;
    transform: translateY(0);
}

.overlay-left {
    transform: translateY(-20%);
}


.dowebok.right-panel-active .sign-in-container {
    transform: translateY(100%);
}


.dowebok.right-panel-active .overlay-container {
    transform: translateX(-100%);
}


.dowebok.right-panel-active .sign-up-container {
    transform: translateX(100%);
    opacity: 1;
    z-index: 5;
}


.dowebok.right-panel-active .overlay {
    transform: translateX(50%);
}


.dowebok.right-panel-active .overlay-left {
    transform: translateY(0);
}


.dowebok.right-panel-active .overlay-right {
    transform: translateY(20%);
}



</style> 

